#订单中的商品
from alipay import AliPay
from rest_framework import serializers

from goods.serializers import GoodsListSerializers
from libs.snowflake.idworker import myworker
from taotao.settings import  ali_pub_key_path, ali_private_key_path
from trade.models import OrderGoods, OrderInfo
from trade.utils import ali_init
from user.models import UserProfile


class OrderGoodsSerialzier(serializers.ModelSerializer):
    goods = GoodsListSerializers()
    class Meta:
        model = OrderGoods
        fields = "__all__"

#订单商品信息
# goods字段需要嵌套一个OrderGoodsSerializer
class OrderDetailSerializer(serializers.ModelSerializer):
    good = OrderGoodsSerialzier(many=True)
    # 支付订单的url
    alipay_url = serializers.SerializerMethodField(read_only=True)

    def get_alipay_url(self, obj):
        alipay = ali_init()

        url = alipay.api_alipay_trade_page_pay(
            subject=obj.order_sn,
            out_trade_no=obj.order_sn,
            total_amount=obj.order_amount,
            return_url='http://192.168.1.6:8001/#/paysuccess'
        )
        pay_url = "https://openapi.alipaydev.com/gateway.do?{data}".format(data=url)

        return pay_url

    class Meta:
        model = OrderInfo
        fields = "__all__"


class OrderSerializer(serializers.ModelSerializer):
    # user = serializers.HiddenField(
    #     default=serializers.CurrentUserDefault(),required=False
    # )
    #user = UserProfile.objects.get(username='aaa')
    #good = OrderGoodsSerialzier()
    #生成订单的时候这些不用post
    pay_status = serializers.CharField(read_only=True)
    trade_no = serializers.CharField(read_only=True)
    order_sn = serializers.CharField(read_only=True)
    pay_time = serializers.DateTimeField(read_only=True)
    nonce_str = serializers.CharField(read_only=True)
    pay_type = serializers.CharField(read_only=True)
    #支付订单的url
    alipay_url = serializers.SerializerMethodField(read_only=True)

    def get_alipay_url(self, obj):
        alipay = ali_init()

        print('-------',obj.order_sn)
        print('-------', obj.order_amount)
        url = alipay.api_alipay_trade_page_pay(#wap_pay
            subject=obj.order_sn,
            out_trade_no=obj.order_sn,
            total_amount=obj.order_amount,
            return_url='http://192.168.1.5:8001/#/paysuccess'
        )
        pay_url = "https://openapi.alipaydev.com/gateway.do?{data}".format(data=url)

        return pay_url


    def generate_order_sn(self):
        #生成订单号
        order_sn = myworker.get_id()
        # 当前时间+userid+随机数
        # from random import Random
        # random_ins = Random()
        # order_sn = "{time_str}{userid}{ranstr}".format(time_str=time.strftime("%Y%m%d%H%M%S"),
        #                                                userid=self.context["request"].user.id,
        #                                                ranstr=random_ins.randint(10, 99))
        return order_sn

    def validate(self, attrs):
        #validate中添加order_sn，然后在view中就可以save
        attrs["order_sn"] = self.generate_order_sn()
        return attrs

    class Meta:
        model = OrderInfo
        fields = "__all__"

